package ar.edu.unq.wannaplay.panels.project;

import java.util.List;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.PropertyModel;
import org.joda.time.Interval;

import ar.edu.unq.wannaplay.dtos.ClientDTO;
import ar.edu.unq.wannaplay.exceptions.NegativeNumberException;
import ar.edu.unq.wannaplay.exceptions.NullArgumentException;
import ar.edu.unq.wannaplay.pages.project.EditProjectEmployeesPage;
import ar.edu.unq.wannaplay.pages.project.EditProjectSkillsPage;
import ar.edu.unq.wannaplay.pages.project.ProjectsViewPage;
import ar.edu.unq.wannaplay.panels.AbstractBasicEditPanel;
import ar.edu.unq.wannaplay.panels.DateFromAndToPanel;
import ar.edu.unq.wannaplay.panels.FeedBackPanel;
import ar.edu.unq.wannaplay.services.ProjectService;
import ar.edu.unq.wannaplay.utils.DateCollector;
import ar.edu.unq.wannaplay.utils.ProjectEditer;
import ar.edu.unq.wannaplay.utils.WPLinkCreatedPage;

public class EditProjectPanel extends AbstractBasicEditPanel<ProjectEditer, ProjectService> {

    private static final long serialVersionUID = 1L;

    private DateCollector dateColector;

    private ClientDTO projectClient;

    public EditProjectPanel(final String id, final ProjectEditer modelObject, final ProjectService service,
            final FeedBackPanel feedBackPanel) {
        super(id, modelObject, service, feedBackPanel);
        projectClient = modelObject.getClient();
    }

    @Override
    protected void addComponents() {
        this.addIntervalEditer();
        this.addChoiceClient();
        this.addLinks();

    }

    protected void addLinks() {
        EditProjectEmployeesPage pageToLink = new EditProjectEmployeesPage(this.getModelObject(), this.getService());
        WPLinkCreatedPage editProjectEmployees = new WPLinkCreatedPage("editProjectEmployees", pageToLink);
        EditProjectSkillsPage pageSkills = new EditProjectSkillsPage(this.getModelObject(), this.getService());
        WPLinkCreatedPage editSkills = new WPLinkCreatedPage("editProjectSkills", pageSkills);
        this.add(editSkills);
        this.add(editProjectEmployees);
    }

    protected void addChoiceClient() {

        PropertyModel<ClientDTO> clientBind = new PropertyModel<ClientDTO>(this, "projectClient");
        List<ClientDTO> clients = this.getService().findAllClientDTOs();

        DropDownChoice<ClientDTO> choices = new DropDownChoice<ClientDTO>("clientChoice", clientBind, clients);
        choices.setChoiceRenderer(new IChoiceRenderer<ClientDTO>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Object getDisplayValue(final ClientDTO object) {
                return object.getName();
            }

            @Override
            public String getIdValue(final ClientDTO object, final int index) {
                return String.valueOf(index);
            }

        });

        choices.setNullValid(true);
        this.getForm().add(choices);
    }

    @Override
    protected String[] getFieldsStrings() {
        return new String[] { "name", "manHour" };
    }

    @Override
    protected void acceptAction() {
        try {
            this.errorWrapedAcceptAction();
        } catch (NullArgumentException e) {
            this.getFeedBackPanel().setMessage(this.getLocalizer().getString("error.nullcombobox", this));
        } catch (NegativeNumberException e) {
            this.getFeedBackPanel().setMessage(this.getLocalizer().getString("error.negativeNumber", this));
        }
    }

    protected void errorWrapedAcceptAction() throws NullArgumentException, NegativeNumberException {
        this.getService().updateProject(this.getModelObject().getId(), this.getModelObject().getManHour(),
                this.getModelObject().getName(), this.getInterval().toInterval(), this.getProjectClient());
        this.setResponsePage(ProjectsViewPage.class);
    }

    protected void addIntervalEditer() {
        this.setInterval(this.prepareDateColector());
        this.getForm().add(new DateFromAndToPanel("dateInterval", this.getInterval()));
    }

    protected DateCollector prepareDateColector() {
        DateCollector colector = new DateCollector();
        Interval modelInterval = this.getModelObject().getInterval();
        colector.setStart(modelInterval.getStart().toDate());
        colector.setEnd(modelInterval.getEnd().toDate());
        return colector;
    }

    public void setInterval(final DateCollector aDateColector) {
        this.setDateColector(aDateColector);
    }

    public DateCollector getInterval() {
        return this.getDateColector();
    }

    public void setProjectClient(final ClientDTO projectClient) {
        this.projectClient = projectClient;
    }

    public ClientDTO getProjectClient() {
        return projectClient;
    }

    public void setDateColector(final DateCollector dateColector) {
        this.dateColector = dateColector;
    }

    public DateCollector getDateColector() {
        return dateColector;
    }

}
